package com.cjk.console.ctrl.sys;

import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cjk.console.SessionApi;
import com.cjk.console.model.LoginInfo;
import com.cjk.foundation.commons.BusinessException;
import com.cjk.foundation.commons.response.Response;
import com.cjk.foundation.dao.EmployeeDao;
import com.cjk.foundation.dao.MenuDao;
import com.cjk.foundation.entity.EmployeeEntity;
import com.cjk.foundation.entity.MenuEntity;
import com.cjk.foundation.enums.IsStatus;
import com.cjk.foundation.util.PasswordUtil;

@Controller
@RequestMapping("/portal")
public class PortalController {
	
	@Autowired
	private MenuDao menuDao;
	
	@Autowired
	private EmployeeDao employeeDao;
	
	@RequestMapping(value = "/login.do", method = RequestMethod.POST)
	@ResponseBody
	public Response login(String username, String password, String salt, HttpSession session){
		EmployeeEntity employeeEntity = employeeDao.selectByUsername(username);
		if(employeeEntity == null){
			throw new BusinessException("用户不存在");
		}
		byte[] pwd = PasswordUtil.encrypt(salt.getBytes(), employeeEntity.getPassword());
		if(!MessageDigest.isEqual(PasswordUtil.hexStringToBytes(password), pwd)){
			throw new BusinessException("密码错误");
		}
		LoginInfo loginInfo = new LoginInfo();
		loginInfo.setId(employeeEntity.getId());
		loginInfo.setRoleId(employeeEntity.getRoleId());
		loginInfo.setRoleName(employeeEntity.getRoleName());
		loginInfo.setUsername(employeeEntity.getUsername());
		SessionApi.setLoginUserInfo(session, loginInfo);
		session.setMaxInactiveInterval(-1);
		return Response.success();
	}
	
	@RequestMapping(value = "/logout.do", method = RequestMethod.GET)
	@ResponseBody
	public Response logout(HttpSession session){
		SessionApi.removeLoginUserInfo(session);
		return Response.success();
	}
	
	@RequestMapping(value = "/getLoginInfo.do", method = RequestMethod.POST)
	@ResponseBody
	public Response getLoginInfo(HttpSession session){
		return Response.success(SessionApi.getLoginUserInfo(session));
	}
	
	@RequestMapping(value = "/getMenu.do", method = RequestMethod.POST)
	@ResponseBody
	public Response getMenu(HttpSession session){
		LoginInfo loginInfo = SessionApi.getLoginUserInfo(session);
		List<MenuEntity> menuEntities = new ArrayList<>();
		menuEntities = getMenuList(menuEntities, 0, loginInfo.getRoleId());
		return Response.success(menuEntities);
	}
	
	private List<MenuEntity> getMenuList(List<MenuEntity> menuEntities, Integer parentId, Integer roleId){
		menuEntities = menuDao.selectMenuListByParentId(parentId, roleId, IsStatus.TRUE);
		for(MenuEntity menuEntity : menuEntities){
			List<MenuEntity> subMenuList = new ArrayList<>();
			subMenuList = getMenuList(subMenuList, menuEntity.getId(), roleId);
			menuEntity.setSubMenuList(subMenuList);
		}
		return menuEntities;
	}
	
	@RequestMapping(value = "/getZTree.do", method = RequestMethod.POST)
	@ResponseBody
	public Response getZTree(Integer roleId){
		if(roleId == null){
			return Response.success(menuDao.selectZTree());
		}else{
			return Response.success(menuDao.selectZTreeByRoleId(roleId));
		}
	}
	
}
